# This file is part of BlackArch Linux ( https://www.blackarch.org/ ).
# See COPYING for license details.

pkgbase=oletools
pkgname=('python2-oletools' 'python-oletools')
pkgver=0.60.2
pkgrel=2
epoch=1
groups=('blackarch' 'blackarch-binary' 'blackarch-forensic')
pkgdesc='Tools to analyze Microsoft OLE2 files.'
arch=('any')
url='https://pypi.org/project/oletools/'
license=('GPL2')
makedepends=('python2-setuptools' 'python-setuptools')
source=("https://github.com/decalage2/oletools/archive/refs/tags/v$pkgver.tar.gz")
sha512sums=('d555d329c7949ed2635813975536397b69e0f7c7e2846587bf844796c4cc988eab95aeb51abd672fdf2eb270329cd6c69ced3f8c41a28f0d8e8dc2be13e6908f')

prepare() {
  cd "$pkgbase-$pkgver"

  sed -i 's|pyparsing>=2.1.0,<3|pyparsing|g' setup.py
  sed -i 's|pyparsing>=2.1.0,<3|pyparsing|g' requirements.txt

  cd $srcdir

  cp -a "$pkgbase-$pkgver"{,-2}
}

build() {
  cd "$pkgbase-$pkgver-2"

  python2 setup.py build

  cd "$srcdir/$pkgbase-$pkgver"

  python setup.py build
}

package_python2-oletools() {
  depends=('python2' 'python2-colorclass' 'python2-pyparsing' 'python2-olefile'
           'python2-easygui' 'python2-pcodedmp' 'python2-msoffcrypto-tool')

  cd "$pkgbase-$pkgver-2"

  python2 setup.py install --root="$pkgdir" --prefix=/usr -O1 --skip-build

  install -Dm 644 -t "$pkgdir/usr/share/doc/${pkgname[0]}/" README.md
  install -Dm 644 oletools/LICENSE.txt \
    "$pkgdir/usr/share/licenses/${pkgname[0]}/LICENSE"

  # fixing shebang line
  cd "$pkgdir/usr/lib/python2.7/site-packages/$pkgbase/"
  for file in *.py; do
    if [ "${file}" != "__init__.py" ]; then
      sed -i '1s/python/python2/' "${file}"
    fi
  done

  for t in "$pkgdir/usr/bin/"* ; do
    mv $t "${t}2"
  done
}

package_python-oletools() {
  depends=('python' 'python-colorclass' 'python-pyparsing' 'python-olefile'
           'python-easygui' 'python-pcodedmp' 'python-msoffcrypto-tool')

  cd "$pkgbase-$pkgver"

  python setup.py install --root="$pkgdir" --prefix=/usr -O1 --skip-build

  install -Dm 644 -t "$pkgdir/usr/share/doc/$pkgbase/" README.md
  install -Dm 644 oletools/LICENSE.txt \
    "$pkgdir/usr/share/licenses/$pkgbase/LICENSE"

  cp oletools/doc/* "$pkgdir/usr/share/doc/$pkgbase"
}

